package mnstudent.global;

import mnstudent.presentationlayer.frmMainMenu;
import mnstudent.security.AES;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author Administrator
 */
public class SConnection {

    public static String URL;
    public static String DriverName;
    public static String Host;
    public static String Port;
    public static boolean IntegratedSecurity;
    public static String UserName;
    public static String Password;
    public static String DatabaseName;
    public static String Others;

    public static String strURL;

    private static SAXParserFactory saxParserFactory;
    private static SAXParser saxParser;
    public static AES aes;
    public static final String _FILE_CONFIG_ = "xml/connection.xml";

    public static void Init() {
        try {
            aes = new AES();
        } catch (AES.EncryptDecryptException ex) {
            Logger.getLogger(SConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        load();
    }

    public static void load() {
        try {
            saxParserFactory = SAXParserFactory.newInstance();
            saxParser = saxParserFactory.newSAXParser();
            ConnectionsHandler handler = new ConnectionsHandler();
            saxParser.parse(new File(_FILE_CONFIG_), handler);

            List<Connection> lstConnection = handler.getLstConnections();

            URL = lstConnection.get(0).getURL();
            DriverName = lstConnection.get(0).getDriverName();
            Host = lstConnection.get(0).getHost();
            Port = lstConnection.get(0).getPort();
            IntegratedSecurity = lstConnection.get(0).isIntegratedSecurity();
            UserName = lstConnection.get(0).getUserName();
            if (!lstConnection.get(0).getPassword().trim().equals("")) {
                Password = aes.decrypt(lstConnection.get(0).getPassword());
            }else{
                Password = lstConnection.get(0).getPassword();
            }
            DatabaseName = lstConnection.get(0).getDatabaseName();
            Others = lstConnection.get(0).getOthers();

            strURL = URL;
            strURL = String.format(strURL, Host, Port, DatabaseName);
            strURL += "%s%s";
            String strIntegratedSecurity;
            if (!URL.equals("jdbc:sqlserver://%s:%s;databaseName=%s")) {
                strIntegratedSecurity = ";user=%s;password=%s";
            } else {
                if (!IntegratedSecurity) {
                    strIntegratedSecurity = ";user=%s;password=%s";
                } else {
                    strIntegratedSecurity = ";integratedSecurity=true";
                }
            }
            String temp = String.format(strURL, strIntegratedSecurity, Others);
            strURL = String.format(temp, UserName, Password).trim();

        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(frmMainMenu.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AES.EncryptDecryptException ex) {
            Logger.getLogger(SConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
